package com.xindacloud.sms.console.system.service.impl;

import java.lang.reflect.InvocationTargetException;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.console.result.Result;
import com.xindacloud.sms.console.result.ResultGenerator;
import com.xindacloud.sms.console.system.dto.SmsFriendshipLinkDTO;
import com.xindacloud.sms.console.system.entity.SmsAgent;
import com.xindacloud.sms.console.system.entity.SmsFriendshipLink;
import com.xindacloud.sms.console.system.mapper.SmsFriendshipLinkMapper;
import com.xindacloud.sms.console.system.service.ISmsFriendshipLinkService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czc
 * @since 2019-10-14
 */
@Service
public class SmsFriendshipLinkServiceImpl extends ServiceImpl<SmsFriendshipLinkMapper, SmsFriendshipLink> implements ISmsFriendshipLinkService {

	@Override
	public Result getSmsFriendshipLinkList(SmsFriendshipLinkDTO smsFriendshipLinkDTO) {
		IPage<SmsFriendshipLink> page=new Page<>(smsFriendshipLinkDTO.getPageNum(),smsFriendshipLinkDTO.getPageSize());
		QueryWrapper<SmsFriendshipLink> queryWrapper=new QueryWrapper<>();
		if(smsFriendshipLinkDTO.getName()!=null) {
		queryWrapper.eq("name", smsFriendshipLinkDTO.getName());
		}
		return ResultGenerator.genSuccessResult(this.page(page,queryWrapper));
	}

	@Override
	public Result saveOrUpdateSmsFriendshipLink(SmsFriendshipLinkDTO smsFriendshipLinkDTO) {
		SmsFriendshipLink smsFriendshipLink=new SmsFriendshipLink();
		try {
			BeanUtils.copyProperties(smsFriendshipLinkDTO,smsFriendshipLink);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(this.saveOrUpdate(smsFriendshipLink)){
		    return ResultGenerator.genSuccessResult("保存或修改成功！");
		}else {
			return ResultGenerator.genClientFailResult("保存或修改失败！");
		}
	}

	@Override
	public Result deleteSmsFriendshipLink(SmsFriendshipLinkDTO smsFriendshipLinkDTO) {
		
		if(this.removeById(smsFriendshipLinkDTO.getId())){
		    return ResultGenerator.genSuccessResult("删除成功！");
		}else {
			return ResultGenerator.genClientFailResult("删除失败！");
		}
	}

}
